//
//  LIkeGongLvViewController.m
//  礼物说
//
//  Created by dllo on 16/5/4.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "LIkeGongLvViewController.h"

@interface LIkeGongLvViewController ()<UICollectionViewDataSource , UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, assign) NSInteger judge;
@property (nonatomic, retain) UIBarButtonItem * button;
@property (nonatomic, assign) NSInteger count;

@end

@implementation LIkeGongLvViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc{
    
    [_button release];
    [_collectionView release];
    [_array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"喜欢的攻略";
    self.tabBarController.tabBar.hidden = YES;

    self.button = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction)];
    
    self.navigationItem.rightBarButtonItem = _button;
    [_button release];
    
    self.judge = 1;
    
    [self createCollectionView];
}
- (void)buttonAction{
    
    if (_count % 2 == 0) {
        self.judge = 2;
        _button.title = @"正常";
        
    }else{
        self.judge = 1;
        _button.title = @"删除";
    }
    _count ++;
    
}

#pragma mark - 创建CollectionView
- (void)createCollectionView{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake(kWIDTH - 20, kWIDTHS(200));
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 49) collectionViewLayout:layout];
    [self.view addSubview:_collectionView ];
    [_collectionView release];
    [layout release];
    
    _collectionView.pagingEnabled = NO;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[LikeGongLvCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LikeGongLvCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _array[indexPath.row];
    return cell;
}

#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_judge == 1) {
        
        GongLvXiangQingViewController * Vc = [[GongLvXiangQingViewController alloc] init];
        gongLv * model = _array[indexPath.row];
        Vc.gongLvNumber = (NSNumber *)model.url;
        Vc.judge = @"收藏";
        [self.navigationController pushViewController:Vc animated:YES];
        [Vc release];
    }else{
        
        gongLv * gonglv = [[[gongLv alloc] init]autorelease];
        gonglv = _array[indexPath.row];
        [[DatabaseHandle shareDataBase] deleteDataWithGongLV:gonglv Name:gonglv.name];
        [_array removeObjectAtIndex:indexPath.row];
        [_collectionView reloadData];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
